package com.lanou3g.farmdoctor.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.android.volley.toolbox.ImageLoader;
import com.android.volley.toolbox.NetworkImageView;
import com.lanou3g.farmdoctor.R;
import com.lanou3g.farmdoctor.entity.ForRichEntity;
import com.lanou3g.farmdoctor.entity.greendao.RichDetailEntity;
import com.lanou3g.farmdoctor.utils.TimeUtils;
import com.lanou3g.farmdoctor.volley.VolleySingleton;

import java.util.ArrayList;
import java.util.List;

/**
 * 本类由: Risky 创建于: 15/11/24.
 */
public class ItemRichListAdapter extends BaseAdapter {
    private List<RichDetailEntity> datas;
    private LayoutInflater inflater;
    private ImageLoader imageLoader;

    public ItemRichListAdapter(Context context) {
        this.inflater = LayoutInflater.from(context);
        imageLoader = VolleySingleton.getImageLoader();
    }

    public void add(List<RichDetailEntity> datas){
        if (this.datas == null) {
            this.datas = new ArrayList<>();
        }
        this.datas.addAll(datas);
        notifyDataSetChanged();
    }

    @Override
    public int getCount() {
        return datas == null ? 0 : datas.size()-1;
    }

    @Override
    public RichDetailEntity getItem(int position) {
        return datas == null ? null : datas.get(position+1);
    }

    @Override
    public long getItemId(int position) {
        return 0;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder = null;
        if (convertView == null) {
            convertView = inflater.inflate(R.layout.item_item_rich,null,false);
            holder = new ViewHolder(convertView);
            convertView.setTag(holder);
        }else {
            holder = (ViewHolder) convertView.getTag();
        }

        RichDetailEntity entity = getItem(position);
        holder.imgHead.setDefaultImageResId(R.mipmap.ic_loading_fail);
        holder.imgHead.setErrorImageResId(R.mipmap.ic_loading_fail);
        holder.imgHead.setImageUrl(entity.getPicUrl(), imageLoader);

        holder.tvTitle.setText(entity.getTitle());
        long timeInMillis = Integer.parseInt(entity.getSendtime());
        String time = TimeUtils.getTime(timeInMillis * 1000);
        holder.tvTime.setText(time);

        holder.tvZhuwen.setText(entity.getCmtcount());
        holder.tvZan.setText(entity.getZancount());

        return convertView;
    }

    public static class ViewHolder{
        private NetworkImageView imgHead;
        private TextView  tvTitle;
        private TextView  tvTime;
        private ImageView imgZhuwen,imgZan;
        private TextView  tvZhuwen,tvZan;

        public ViewHolder(View view){
            imgHead = (NetworkImageView)view.findViewById(R.id.img_main);
            tvTitle = (TextView)view.findViewById(R.id.tv_title);
            tvTime  = (TextView)view.findViewById(R.id.tv_time);

            imgZhuwen = (ImageView) view.findViewById(R.id.img_zhuwen);
            imgZan = (ImageView) view.findViewById(R.id.img_zan);
            tvZhuwen = (TextView) view.findViewById(R.id.tv_zhuwen);
            tvZan = (TextView) view.findViewById(R.id.tv_zan);
        }
    }
}
